import React, { Component } from 'react'
import PropTypes from 'prop-types'

import './index.css'
export default class Footer extends Component {
  state = {
    finishNum:0,
    allNums:0
  }

  static propTypes = {
    todos:PropTypes.array.isRequired,
    checkTodo:PropTypes.func.isRequired,
    
  }
  handleCheck = (event)=>{
    //console.log('footer==>',event.target.checked)
    this.props.checkTodo(event.target.checked)
  }
  handleDeleteAll = ()=>{
    if(window.confirm('delete all done?')){
      this.props.deleteAlldoneTodo()
    }
    
  }
  render() {
    const {todos} = this.props
    //console.log(todos.length)
    //已经完成数
    const finishNum = todos.reduce((pre,todo)=>{
      //console.log('reduce==>',todo)
      if(todo.done===true){
        return pre+1
      }else{
        return pre
      }
      
    },0)

    const total = todos.length
    //this.setState({allNums:todos.length})
    //this.setState({allNums:todos.length})
    return (
      <div className="todo-footer">
        <label>
          <input type="checkbox" onChange={this.handleCheck} checked={(finishNum===total&&total>0)?true:false}/>
        </label>
        <span>
          <span>已完成{finishNum}</span> / 全部{total}
        </span>
        <button  className="btn btn-danger" onClick={this.handleDeleteAll}>清除已完成任务</button>
      </div>
    )
  }
}
